/**
 * Created by Administrator on 17-6-12.
 */
//                                                托管中
var gift_show = [
    { data: "time" },
    { data: function(obj){
        $NTX="<h7>"+obj.boy_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.boy_head_img+"'/><span style='display: none'>"+obj.boy_id+"</span>" +
            "</a>";
        return $NTX;
    } },
    { data: function(obj){
        $TX="<h7>"+obj.girl_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.girl_head_img+"'/><span style='display: none'>"+obj.girl_id+"</span>" +
            "</a>";
        return $TX;
    } },
    { data:function(obj){
            $TX="<a href='Javascript:;' ><img class='pimg' width='80px' height='80px' src='"+obj.product_img+"'/></a>";
      mroe_img ()
            return $TX;
        }},
    {
        data:"product_title"
    },
    { data: "price" },
    { data: function (obj){
        $zt = "";
        if(obj.deposit_state == "0"){
            $zt = "正常"
        }
        if(obj.deposit_state == "1"){
            $zt = "冻结"
        }
        if(obj.deposit_state == "2"){
            $zt = "已处理"
        }
        return $zt
    }}
];
//托管中的 传的值
  var  getData1 =  function (d) {
      d.deliver_type = 1;
  };
$(".liwuGl").click(function () {

    Data_table("#example6",address1+"/admin/Deliver/main",gift_show,true,getData1,5);

});
$("#tuoguan").click(function(){
    Data_table("#example6",address1+"/admin/Deliver/main",gift_show,true,getData1,5);
});
  //  点击头像 查看资料
$("#example6").ajaxComplete(function(){  //意思是在重新加载完了之后再执行函数
    $(".userData").click(function(){
        console.log($(this).find("span").text())
    });
});

//接受中 传的值
var  getData2 =  function (d) {
    d.deliver_type = 2;
};
var gift_show1 =[
    {data:"time"},
    {data: function(obj){
        $sed = "<h7>"+obj.boy_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.boy_head_img+"'/><span style='display: none'>"+obj.boy_id+"</span>" +
            "</a>";
        return $sed
    }},
    {data: function (obj){
        $get ="<h7>"+obj.girl_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.girl_head_img+"'/><span style='display: none'>"+obj.girl_id+"</span>" +
            "</a>";
        return $get
    }
    },
    {data : function(obj){
        $gift_img = "<a href='Javascript:;' ><img class='pimg' width='80px' height='80px' src='"+obj.product_img+"'/></a>";
      mroe_img ()
        return $gift_img
    }},
    {data:"product_title" },
    {data: "price" }
];
$("#Accepted").click(function(){
    Data_table("#example7",address1+"/admin/Deliver/main",gift_show1,true,getData2,5);
});
//需发货 传的值
var  getData3 =  function (d) {
    d.deliver_type = 3;
};
var  gift_show3 = [
    {data: "time"},
    {data: function(obj){
        $sed1 = "<h7>"+obj.boy_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.boy_head_img+"'/><span style='display: none'>"+obj.boy_id+"</span>" +
            "</a>";
        return $sed1
    }},
    {data: function (obj){
        $get1 ="<h7>"+obj.girl_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.girl_head_img+"'/><span style='display: none'>"+obj.girl_id+"</span>" +
            "</a>";
        return $get1
    }
    },
    {data: function(obj){
        $gift_img1 = "<a href='Javascript:;' ><img class='pimg' width='80px' height='80px' src='"+obj.product_img+"'/></a>";
      mroe_img ()
        return $gift_img1
    }},
    {data:"product_title" },
    {data: "price"},
    {data: "receipt_address"},
    {data: "receipt_phone"},
    {data: "receipt_name"},
    {data: function (obj){
        $fahuo1 = "<a href='Javascript:;'  class='btn btn-small fa_huo'  data-toggle='modal' data-target='#giftretun'>发货<span style='display: none'>"+obj.appoint_status_id+"</span></a>";
        return $fahuo1
    }}

];

$("#required").click(function(){
    Data_table("#example8",address1+"/admin/Deliver/main",gift_show3,true,getData3,5);
});

function TJ_ajax_fahuo(url,data,removeModal,table){
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function(data){
            if(data.code==1){
           
                swal("操作成功!", "", "success");
                     $(removeModal).modal("hide");//弹出界面隐藏
                // 重置table
                var tablee = $(table).DataTable();
                tablee.ajax.reload();
            }else{
                swal(data.mag)
            }
        },
        error:function(){
            swal("请求失败");
        }
    });
};


// 已发货
var  getData4 =  function (d) {
    d.deliver_type = 4;
};
var gift_show4 = [
    {data: "time"},  //时间
    {data: function(obj){  //赠送人
        $sed1 = "<h7>"+obj.boy_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.boy_head_img+"'/><span style='display: none'>"+obj.boy_id+"</span>" +
            "</a>";
        return $sed1
    }},
    {data: function (obj){  //接收人
        $get1 ="<h7>"+obj.girl_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.girl_head_img+"'/><span style='display: none'>"+obj.girl_id+"</span>" +
            "</a>";
        return $get1
    }
    },
    {data: function(obj){   //礼物图片
        $gift_img1 = "<a href='Javascript:;' ><img class='pimg' width='80px' height='80px' src='"+obj.product_img+"'/></a>";
      mroe_img ()
        return $gift_img1
    }},
    {data:"product_title" },  //礼物名字
    {data: "price"}, //礼物金额
    {data: "receipt_address"},  //收货地址 express express_number
    {data : function (obj) {//物流单号
    	$kuaidi = "";
    	$kuaidi = "<div><b>"+obj.express+"</b><br/><span>"+obj.express_number+"</span></div>";
    	return $kuaidi
    }},  
    {data: "receipt_phone"},   //电话
    {data: "receipt_name"}   //收货名字
    //{data: function (obj){    //操作
    //    $fahuo1 = "<a href='Javascript:;'  class='btn btn-small fa_huo'  data-toggle='modal' data-target='#giftretun'>发货<span style='display: none'>"+obj.appoint_status_id+"</span></a>";
    //    return $fahuo1
    //}}
];
$("#Shipped").click(function(){
    Data_table("#example9",address1+"/admin/Deliver/main",gift_show4,true,getData4,5);
});
                                                                                      //已处理
var  getData5 =  function (d) {
    d.deliver_type = 6;
};
var gift_show5 = [
    { data: "time" },
    { data: function(obj){
        $NTX="<h7>"+obj.boy_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.boy_head_img+"'/><span style='display: none'>"+obj.boy_id+"</span>" +
            "</a>";
        return $NTX;
    } },
    { data: function(obj){
        $TX="<h7>"+obj.girl_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.girl_head_img+"'/><span style='display: none'>"+obj.girl_id+"</span>" +
            "</a>";
        return $TX;
    } },
    { data:function(obj){
        $TX="<a href='Javascript:;' ><img class='pimg' width='80px' height='80px' src='"+obj.product_img+"'/></a>";
      mroe_img ()
        return $TX;
    }},
    {
        data:"product_title"
    },
    { data: "price" },
    { data :function(obj){
        $kkl ="";
        $kkl = "<span style='color: red'>"+obj.handle_content+"</span>";
        return $kkl
    }}

];
$("#OK").click(function(){

    Data_table("#example10",address1+"/admin/Deliver/main",gift_show5,true,getData5,5);
});
                                                                           //已兑换
var  getData6 =  function (d) {
    d.deliver_type = 5;
};
var gift_show6 = [
    { data: "time" },
    { data: function(obj){
        $NTX="<h7>"+obj.boy_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.boy_head_img+"'/><span style='display: none'>"+obj.boy_id+"</span>" +
            "</a>";
        return $NTX;
    } },
    { data: function(obj){
        $TX="<h7>"+obj.girl_nick+"</h7><br/><a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>" +
            "<img width='80px' height='80px' src='"+obj.girl_head_img+"'/><span style='display: none'>"+obj.girl_id+"</span>" +
            "</a>";
        return $TX;
    } },
    { data:function(obj){
        $TX="<a href='Javascript:;' ><img class='pimg' width='80px' height='80px' src='"+obj.product_img+"'/></a>";
         mroe_img ()
        return $TX;
    }},
    {
        data:"product_title"
    },
    { data: "price" }
];
$("#Converted").click(function(){
    Data_table("#example12",address1+"/admin/Deliver/main",gift_show6,true,getData6,5);
});
                                                                                                                    //投诉管理
var getData7 = {

};
var gift_show7 = [
    { data: "complain_time" },   //时间
    { data: function  (obj) {        //投诉人
        $mm="";
        $mm = "<a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>"+obj.boy_nick+"<span style='display: none;'>"+obj.touId+"</span></a>";
        return $mm
    }},
    { data:  function  (obj) {        //被投诉人
        $lm="";
        $lm = "<a href='Javascript:;' class='userData' data-toggle='modal' data-target='#myModal500'>"+obj.girl_nick+"<span style='display: none;'>"+obj.beiId+"</span></a>";
        return $lm
    }},
    { data: "complain_reason" },  //原因
    { data:  function  (obj) {  // 图片
        $ii = "";
        if(obj.img_url != ""){

            $ii = "<img src='"+obj.img_url+"' style ='width:80px;height:80px'  class = 'pimg'/>";
            $(".pimg").click(function(){
                var _this = $(this);//将当前的pimg元素作为_this传入函数
                imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
            });
            return $ii
        }else{
            $ii = "<span>无图片</span>";
            return $ii
        }
    } },
    {data:"complain_content"},     //内容
    { data: function(obj){                 //操作 (如果状态为1的时候有操作 如果状态为2的时候 显示处理内容)
        if(obj.complain_state == 1){
            $xiox="<div><a href='Javascript:;' class='btn btn-small tu_cl' value='1'   data-toggle='modal' data-target='#chuli'>解冻</a><a href='Javascript:;'  class='btn btn-small tu_cl giftretun' value='2'   data-toggle='modal' data-target='#chuli'>处理</a></div><span style='display: none'>"+obj.appoint_status_id+"</span>";
            return $xiox;
        }if(obj.complain_state == 2){
            $centee = "";
            $centee ="<span>"+obj.handle_content+"</span>";
            return $centee
        }

    } }
];
$("#giftcomplaints").click(function(){
    Data_table("#example1_gift",address1+"/admin/Deliver/Complain",gift_show7,false,getData7,5);
});
//导出BIAO
var tableToExcel = (function () {
    var uri = 'data:application/vnd.ms-excel;base64,',
        template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">'
            + '<head><meta http-equiv="Content-type" content="text/html;charset=UTF-8" /><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/>'
            + '</x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
        base64 = function (s) {
            return window.btoa(unescape(encodeURIComponent(s)))
        },
        format = function (s, c) {
            return s.replace(/{(\w+)}/g, function (m, p) {
                return c[p];
            })
        };

    return function (table, name) {
        var ctx = {
            worksheet: name || 'Worksheet',
            table: table.innerHTML
        };
        return uri + base64(format(template, ctx));
    }
})();


$('#exportExcel').on('click', function () {
    var tableBlock = ["#example6", "#example7", "#example8", "#example9", "#example10", "#example11", "#example12","#example1_gift"];
    var whereTable=["#tuoguan","#Accepted","#required","#Shipped","#OK","#Returned","#Converted","#giftcomplaints"];
    var hoq = "";var table_cl="";
    $.each($(".table-data"), function (i) {
        //判断每一个div，其css中display是否为block
        if ($(this).css("display") == "block") {
            console.log(i);
            //return tableBlock[i]
            hoq = tableBlock[i];
            table_cl=$(whereTable[i]).text();
        }
    });
    var $this = $(this);
    //设定下载的文件名及后缀
    $this.attr('download', table_cl+' 报表.xls');
    //设定下载内容
    $this.attr('href', tableToExcel($(hoq)[0], '礼物报表'));
});

                                                                                         //托管中 操作
$("#example1_gift").ajaxComplete(function() {         //获取是冻结 还是 处理 并获取 id
    var  appoint_id = "",value="",text22="";
    $(".tu_cl").unbind("click").on("click",function(){
        $("#chuli").modal("hide");
        $("#chuli_content").val("");
        appoint_id = $(this).parent().next().text();
        value = $(this).attr("value");
        text22 = $(this).text();
        var ssssd = {
            "deposit_state":value ,
            "appoint_status_id" : appoint_id
        };
        var JSONstr1 = JSON.stringify(ssssd);
        $("#myModalLabel000").text("是否要"+text22+"本条数据?");
        $("#chuli_cose").append("<span style='display: none'>"+JSONstr1+"</span>");
        $("#chuli_cose").unbind("click").on("click",function(){
            ssssd.handle_content = $("#chuli_content").val();
            TJ_ajaxfor_data(address1+"/admin/Deliver/setDepositState","#chuli",ssssd,"#example1_gift");
        })

        });
});

                                                                                         //待发货 操作

$("#example8").ajaxComplete(function() {
    var  appoint_id = "";
    $(".fa_huo").click(function () {
        appoint_id = $(this).find("span").text();
        $("#truefahuo").click(function(){
            var kdd = $("#fieradd").val();
            var kdh = $("#fierhao").val();
            var kuaidi = {
                "express" : kdd,
                "express_number" : kdh,
                "appoint_status_id" : appoint_id
            };
            TJ_ajax_fahuo(address1+"/admin/Deliver/send",kuaidi,"#giftretun","#example8")
        });
    });
});


function TJ_ajaxfor_data(url,removeModal,data,tables){
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function(data){
            if(data.code==1){
                var table = $(tables).DataTable();
                table.ajax.reload();
                $(removeModal).modal("hide");//弹出界面隐藏
                $(".remove-child").remove();//删除这条数据
                swal("操作成功!", "", "success");
            }else{
                swal(data.data)
            }
        },
        error:function(){
            swal("请求失败");
        }
    });
};